<template>
  <div class="echarts-wrapper">
    <div ref="charts" :style="`height:${height};width:${width}`"></div>
  </div>
</template>

<script>
/* 说明 初始化 必须给options 赋值一次 */
import conf from "./conf";
import resize from "./resize";
import { syncLoadJS } from "../../lib";
export default {
  mixins: [resize],
  props: {
    options: {
      type: Object
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "100%"
    },
    cdn: {
      type: String,
      default: "v1/js/echarts.min.js"
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    options(val) {
      this.initEcharts(this.$refs.charts, val);
    }
  },
  methods: {
    async initEcharts(el) {
      if (!window.BMap) {
        await syncLoadJS(this.cdn);
      }
      if (!this.chart) {
        this.chart = window.echarts.init(el);
      }
      this.chart && this.chart.setOption({ ...conf, ...this.options });
    }
  },
  beforeDestroy() {
    // 销毁图表实例，避免内存溢出
    this.chart && this.chart.dispose && this.chart.dispose();
  },
  async mounted() {
    // 调用utils来绘制图形
    this.initEcharts(this.$refs.charts, this.options);
  }
};
</script>
<style lang="scss" scoped>
.echarts-wrapper {
  // height: 100%;
  // width: 100%;
}
</style>
